package binarysearch.搜索旋转排序数组;

public class Solution {
    public int search(int[] nums, int target) {
        return searchIndex(nums, 0, nums.length - 1, target);
    }

    int searchIndex(int[] nums, int start, int end, int target) {
        if (start > end) {
            return -1;
        }
        int mid = (start + end) / 2;

        if (nums[start] <= nums[mid]) {
            if (nums[start] <= target && target < nums[mid]) {
                return searchIndex(nums, start, mid - 1, target);
            } else {
                return searchIndex(nums, mid + 1, end, target);
            }
        } else {
            if (nums[end] >= target && nums[mid] < target) {
                return searchIndex(nums, mid + 1, end, target);
            } else {
                return searchIndex(nums, start, mid - 1, target);
            }
        }

    }


    public static void main(String[] args) {
        Solution solution = new Solution();
        System.out.println(solution.search(new int[]{4, 5, 6, 7, 8, 0, 1, 2}, 0));
    }
}
